import 'dart:io';
import 'dart:ui';

import 'package:boss/channel/install/install.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:store_redirect/store_redirect.dart';

enum DownloadButtonStatus {
  normal,
  downloading,
  failed,
}

class DownloadButton extends StatefulWidget {
  final String title;
  final String downloadUrl;
  final double normalWidth;
  final double normalHeight;
  final Color color;

  const DownloadButton({
    this.title,
    this.normalWidth,
    this.normalHeight,
    this.color,
    @required this.downloadUrl,
    Key key,
  }) : super(key: key);

  @override
  DownloadButtonState createState() => DownloadButtonState();
}

class DownloadButtonState extends State<DownloadButton> with SingleTickerProviderStateMixin {
  double get width => widget.normalWidth ?? 400.w;

  double get height => widget.normalHeight ?? 80.w;

  double get animationWidth {
    return lerpDouble(width, height, animatedContainer.value);
  }

  bool get isDownloading {
    return animatedContainer.status == AnimationStatus.completed;
  }

  String get buttonText {
    if (animatedContainer.status == AnimationStatus.dismissed) {
      return widget.title;
    }

    return '$progress';
  }

  AnimationController animatedContainer;

  int progress = 0;

  Download download;

  @override
  void initState() {
    super.initState();

    animatedContainer = AnimationController(
      duration: Duration(milliseconds: 150),
      vsync: this,
    )
      ..addListener(() {
        setState(() {});
      })
      ..addStatusListener((status) {});
  }

  @override
  void dispose() {
    animatedContainer.dispose();
    download?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: eventHandle,
      child: Container(
        width: animationWidth,
        height: height,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          color: widget.color ?? Theme.of(context).primaryColor,
          borderRadius: BorderRadius.circular(height),
        ),
        child: Text(
          buttonText,
          style: TextStyleConstant().white_32,
        ),
      ),
    );
  }

  eventHandle() {
    if (Platform.isAndroid) {
      if (animatedContainer.status == AnimationStatus.dismissed) {
        animatedContainer.forward();
        upVersion();
      }
    } else if (Platform.isIOS) {
      StoreRedirect.redirect(iOSAppId: "1492029207");
    }
  }

  animate() {
    if (animatedContainer.status == AnimationStatus.dismissed) animatedContainer.forward();
  }

  resetAnimate() {
    animatedContainer.reverse();
  }

  upVersion() async {
    download = Download();

    download.download(
      widget.downloadUrl,
      onProgressChanged: (progress) {
        if (mounted)
          setState(() {
            this.progress = progress.toInt();
          });
      },
      onCompleted: (filePath) async {
        try {
          print("install path:$filePath");

          Install.install(filePath);
        } catch (e) {
          toast('安装失败');
        }
      },
    );
  }
}
